﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using ModelLibrary;
using System.Text;

namespace WebC
{
    public partial class buy_app : clsBaseForm
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Ajax.Utility.RegisterTypeForAjax(typeof(buy_app));
            if (!IsPostBack)
            {
                loadInfo();
            }
        }

        public string sFileName = "appfile";

        #region 载入信息

        public void loadInfo()
        {
            BuyDevice buy = null;
            string sModelName = "";
            DataSet ds = null;
            divAudi.Visible = false;
            try
            {
                buy = new BuyDevice();
                if (m_Id == 0)
                {
                    if (!checkModelRightById(205, ref sModelName, (clsBaseLib)buy))
                    {
                        errorUrl(string.Format("你没有“{0}”的权限,请与管理员联系！", sModelName));
                        return;
                    }
                    DataTable dt = buy.getUserDept(iUserId);
                    txtDeptId.Text = Convert.ToString(dt.Rows[0]["deptid"]);
                    txtDeptName.Text = Convert.ToString(dt.Rows[0]["deptname"]);                    
                }
                else
                {
                    if (Convert.ToInt32(Request["audi"]) == 1)
                    {
                        if (!checkModelRightById(208, ref sModelName, (clsBaseLib)buy))
                        {
                            errorUrl(sModelName);
                            return;
                        }
                        trbtn.Visible = false;
                        divAudi.Visible = true;
                    }
                    else if (Convert.ToInt32(Request["noedit"]) == 1)
                    {
                        if (!checkModelRightById(204, ref sModelName, (clsBaseLib)buy))
                        {
                            errorUrl(sModelName);
                            return;
                        }
                    }
                    
                    else
                    {
                        if (!checkModelRightById(207, ref sModelName, (clsBaseLib)buy))
                        {
                            errorUrl(sModelName);
                            return;
                        }
                    }
                    ds = buy.getAppInfoById(m_Id);
                }
                sModelFullName = sModelName;

            }
            catch (Exception e)
            {
                errorUrl(Convert.ToString(e));
            }
            finally
            {
                if (buy != null)
                {
                    buy.Dispose();
                }
            }
            if (m_Id > 0)
            {
                if (ds != null)
                {
                    DataTable dt = ds.Tables[0];
                    if (dt.Rows.Count == 1)
                    {
                        txtAppCode.Text = Convert.ToString(dt.Rows[0]["appcode"]);
                        txtAllowDate.Text = Convert.ToDateTime(dt.Rows[0]["allowdate"]).ToString("yyyy-MM-dd");
                        txtDeptId.Text = Convert.ToString(dt.Rows[0]["deptid"]);
                        txtDeptName.Text = Convert.ToString(dt.Rows[0]["deptname"]);
                        txtMemo.Text = Convert.ToString(dt.Rows[0]["memo"]);
                        txtReason.Text = Convert.ToString(dt.Rows[0]["reason"]);
                        lbAppDate.Text = Convert.ToString(dt.Rows[0]["appdate"]);
                        lbUserName.Text = Convert.ToString(dt.Rows[0]["appuser"]);
                        ddlAudi.SelectedValue = Convert.ToString(dt.Rows[0]["audistate"]);
                        txtAudiMemo.Text = Convert.ToString(dt.Rows[0]["audimemo"]);
                        tdupload.InnerHtml = Convert.ToString(dt.Rows[0]["audifile"]);
                        if (ddlAudi.SelectedValue != "0")
                        {
                            btnAudi.Visible = false;
                        }
                        lbAudiTime.Text = dt.Rows[0]["audidate"] == DBNull.Value ? DateTime.Now.ToString("yyyy-MM-dd") : Convert.ToDateTime(dt.Rows[0]["audidate"]).ToString("yyyy-MM-dd");
                    }
                    dt = ds.Tables[1];
                    int iRowCount = dt.Rows.Count;
                    StringBuilder sbu = new StringBuilder();
                    for (int i = 0; i < iRowCount; i++)
                    {
                        sbu.Append(string.Format(@"<tr>
                        <td align=""left"" width=""120"">
                            <a href=""javascript:"" onclick=""insertdevice(this.parentNode.parentNode)"">+</a> <a href=""javascript:"" onclick=""removedevice(this.parentNode.parentNode)"">-</a><input style=""width: 92px"" type=""text"" onclick=""getdevicetype(this)"" value=""{0}"" /><input style=""width: 64px"" type=""hidden"" value=""{1}"" /></td>
                        <td width=""70"">
                            <input  style=""width: 70px"" type=""text"" value=""{2}"" /></td>
                        <td width=""70"">
                            <input  style=""width: 70px"" type=""text"" value=""{3}"" /></td>
                        <td><select style=""width: 188px"">
                                <option value=""{4}"">{5}</option>
                            </select></td>
                        <td width=""150"">
                            <input style=""width: 140px"" type=""text"" value=""{6}"" /></td>
                    </tr>", dt.Rows[i]["typename"], dt.Rows[i]["typeid"], dt.Rows[i]["devicenum"], dt.Rows[i]["singleprice"], dt.Rows[i]["brandid"], Convert.ToString(dt.Rows[i]["brandname"]) == "" ? "请选择品牌" : dt.Rows[i]["brandname"]
                          , dt.Rows[i]["other"]));
                    }
                    ltrDevice.Text = Convert.ToString(sbu);
                }
            }
            else
            {
                lbAppDate.Text = DateTime.Now.ToString("yyyy-MM-dd");
                lbUserName.Text = sUserName;
                ltrDevice.Text = @"<tr>
                        <td align=""left"" width=""120"">
                            <a href=""javascript:"" onclick=""insertdevice(this.parentNode.parentNode)"">+</a> <a href=""javascript:"" onclick=""removedevice(this.parentNode.parentNode)"">-</a><input style=""width: 92px"" type=""text"" onclick=""getdevicetype(this)"" /><input style=""width: 64px"" type=""hidden"" value=""0"" /></td>
                        <td width=""70"">
                            <input  style=""width: 70px"" type=""text"" /></td>
                        <td width=""70"">
                            <input  style=""width: 70px"" type=""text"" /></td>
                        <td><select style=""width: 188px"">
                                <option selected=""selected"">请选择品牌</option>
                            </select></td>
                        <td width=""150"">
                            <input id=""Text5"" style=""width: 140px"" type=""text"" /></td>
                    </tr>";
            }

        }

        #endregion

        #region 获得产品品牌

        [Ajax.AjaxMethod]
        public string getBrandHtml(int iTypeId)
        {
            BuyDevice buy = null;
            DataTable dt = null;
            try
            {
                buy = new BuyDevice();
                dt = buy.getBrandByDeviceTypeId(iTypeId);
            }
            catch (Exception e)
            {
                return Convert.ToString(e);
            }
            finally
            {
                if (buy != null)
                {
                    buy.Dispose();
                }
            }
            StringBuilder sbu = new StringBuilder();
            sbu.Append(@"<select style=""width: 188px""><option selected=""selected"" value=""0"">请选择品牌</option>");
            int iRowCount = dt.Rows.Count;
            for (int i = 0; i < iRowCount; i++)
            {
                sbu.Append(string.Format(@"<option value=""{0}"">{1}</option>"
                    , dt.Rows[i]["brandid"], dt.Rows[i]["brandname"]));
            }
            sbu.Append(@"</select>");
            return Convert.ToString(sbu);
        }

        #endregion

        int m_Id
        {
            get
            {
                return Convert.ToInt32(Request["id"]);
            }
        }

        #region 保存信息

        [Ajax.AjaxMethod(Ajax.HttpSessionStateRequirement.Read)]
        public bool saveData(int iAppId, string sCode, int iDept, string sAllowDate, string sReason, string sMemo, string[] sDevices)
        {
            BuyDevice buy = null;
            bool bResult = false;
            try
            {
                buy = new BuyDevice();
                sCode = sCode.Replace("'", "''");
                sReason = sReason.Replace("'", "''");
                sMemo = sMemo.Replace("'", "''");
                for (int i = 0; i < sDevices.Length; i++)
                {
                    sDevices[i] = sDevices[i].Replace("'", "''");
                }
                bResult = buy.setDeviceAppInfo(iAppId, iUserId, sCode, iDept, sAllowDate, sReason, sMemo, sDevices);
            }
            catch
            {
                bResult = false;
            }
            finally
            {
                if (buy != null)
                {
                    buy.Dispose();
                }
            }
            return bResult;
        }

        #endregion

        #region 审核申请

        [Ajax.AjaxMethod(Ajax.HttpSessionStateRequirement.Read)]
        public bool audiApp(int iAppId, int iState, string sFile, string sMemo)
        {
            BuyDevice buy = null;
            bool bResult = false;
            try
            {
                buy = new BuyDevice();
                sFile = sFile.Replace("'", "''");
                sMemo = sMemo.Replace("'", "''");
                bResult = buy.audiAppInfoById(iAppId, iUserId, sFile, sMemo, iState);
            }
            catch
            {
                bResult = false;
            }
            finally
            {
                if (buy != null)
                {
                    buy.Dispose();
                }
            }
            return bResult;
        }

        #endregion
    }
}